import { Component, Input, OnInit } from '@angular/core';
import { EventService } from 'src/app/shared/service/event.service';
//tslint:disable
declare let $: any;
@Component({
  selector: 'app-filter-transverse-tag',
  templateUrl: './filter-transverse-tag.component.html',
  styleUrls: ['./filter-transverse-tag.component.scss'],
})
export class FilterTransverseTagComponent implements OnInit {


  @Input() tagData;
  isFirstLoad = true;
  constructor(private eventService: EventService) { }
  paramContent: any;
  ngOnInit() {
    this.eventService.eventEmit.on(this.tagData.name + 'submitClick', (id) => {
      if (id) {
        if (document.getElementById(id)) {
          if (document.getElementById('filterOpenAllButton').style.display === 'flex') {
            this.openAllFilter();
            setTimeout(() => {
              document.getElementById(id).click();
            }, 100);
          } else if (document.getElementById('filterExpendAllButton').style.display === 'flex') {
            this.expandAllFilter();
            setTimeout(() => {
              document.getElementById(id).click();
            }, 100);
          }
        }
      }
    });
  }

  choiceTag(event, k, item) {
    const that = this;
    try {
      if(event){
        event.stopPropagation();
      }
      const array = item.dropDownList.item;
      for (let index = 0; index < array.length; index++) {
        const element = array[index];
        if (index === k) {
          item.dropDownList.item[index].checked = 'true';
          if (this.paramContent === undefined) {
            this.paramContent = { filter: {}, design_case_room: 0, design_case_sys_params_code: 0 };
          }
          this.paramContent.filter[item.name] = item.dropDownList.item[index].value;
          this.paramContent[item.name] = item.dropDownList.item[index].value;
        } else {
          item.dropDownList.item[index].checked = 'false';
        }
      }
      setTimeout(() => {
        that.sendRequest();
      }, 100);
    } catch (error) {
    }
  }

  sendRequest() {
    this.eventService.eventEmit.emit(this.tagData.name + 'AskForList', this.paramContent);
  }

  openAllFilter() {
    this.isFirstLoad = false;
    window['navOneListenerScroll'] = '1';
    // 需要分割滑动的div设置滑动区域
    if (document.getElementsByTagName('app-nav-one')) {
      const navOneCollection = document.getElementsByTagName('app-nav-one') as HTMLCollection;
      for (let i = 0; i < navOneCollection.length; i++) {
        const el = navOneCollection[i];
        if (!this.hasClass(el, 'ion-page-hidden')) {
          const splitDiv = el.getElementsByClassName('scroll-box')[0] as HTMLElement;
          splitDiv.style.removeProperty('height');
          splitDiv.style.removeProperty('overflow');
          splitDiv.style.removeProperty('scroll-behavior');
        }
      }
    }
    document.getElementById('filterTransverseTag').style.display = 'block';
    document.getElementById('filterOpenAllButton').style.display = 'none';
    setTimeout(() => {
      window['navOneListenerScroll'] = '0';
    }, 800);
  }

  expandAllFilter() {
    this.isFirstLoad = false;
    document.getElementById('filterExpendAllButton').style.display = 'none';
    const len = this.tagData.filterItems.length;
    for (let index = 2; index < len; index++) {
      document.getElementById('filterBoxForSection' + index).style.display = 'flex';
    }
    // document.getElementById('filterTransverseTagMainBox').style.display = 'block';
    // 显示筛选       
  }

  /**
 * 元素是否存在值为@{value}的class
 */
  hasClass(ele, value) {
    let has = false;
    if (value) {
      const curValue = this.getClass(ele);
      if (curValue.indexOf(value) > -1) {
        has = true;
      }
    }
    return has;
  }

  /**
 * 获取class
 */
  getClass(elem) {
    return elem.getAttribute && elem.getAttribute("class") || "";
  }
}
